//
//  HNSH_AlipayToors.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/4/9.
//

#import "HNSH_AlipayToors.h"
#import <AlipaySDK/AlipaySDK.h>

@interface HNSH_AlipayToors ()

/* payBlock */
@property(nonatomic, copy) AliPayBlock payBlock;

@end

@implementation HNSH_AlipayToors

+ (HNSH_AlipayToors *)shareToors {
    static HNSH_AlipayToors *toors = nil;
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        toors = [[HNSH_AlipayToors alloc]init];
    });
    return toors;
}

- (void)payOrderWithOrderString:(NSString *)orderString complate:(AliPayBlock)complate {
    _payBlock = complate;
    //应用注册scheme,在AliSDKDemo-Info.plist定义URL types
    NSString *appScheme = @"HHZXSingleProjectSchemes";
    [[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic) {
        NSLog(@"reslut = %@",resultDic);
        if (complate) {
            complate(resultDic);
        }
    }];
}

- (BOOL)alipayApplication:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation {
    if ([url.host isEqualToString:@"safepay"]) {
        //跳转支付宝钱包进行支付，处理支付结果
        WeakSelf(weakSelf);
        [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
            NSLog(@"result = %@",resultDic);
            if (weakSelf.payBlock) {
                weakSelf.payBlock(resultDic);
            }
        }];
    }
    return YES;
}

// NOTE: 9.0以后使用新API接口
- (BOOL)alipayApplication:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options
{
    if ([url.host isEqualToString:@"safepay"]) {
        //跳转支付宝钱包进行支付，处理支付结果
        WeakSelf(weakSelf);
        [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
            NSLog(@"result = %@",resultDic);
            if (weakSelf.payBlock) {
                weakSelf.payBlock(resultDic);
            }
        }];
    }
    return YES;
}

@end
